<template>
  <div class="form-container">
    <el-form :model="user" :rules="rules" ref="updateRef" label-width="80px" class="user-form">
      <div class="form-header">
        修改用户信息
      </div>

      <el-form-item label="用户名">
        <el-input disabled :placeholder="userName"></el-input>
      </el-form-item>

      <el-form-item label="昵称" prop="name">
        <el-input placeholder="请输入昵称" v-model="user.name"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input type="password" placeholder="请输入密码" v-model="user.password"></el-input>
      </el-form-item>

      <el-form-item label="重复密码" prop="confirmPass">
        <el-input type="password" placeholder="请再次输入密码" v-model="user.confirmPass"></el-input>
      </el-form-item>

      <el-form-item label="电子邮箱" prop="email">
        <el-input :placeholder="Email" v-model="user.email"></el-input>
      </el-form-item>

      <el-form-item label="个性签名" prop="signature">
        <el-input style="width: 100%;" placeholder="请输入个性签名" v-model="user.signature"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" class="submit-btn" @click="update">
          提交
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import request from "@/utils/request";
import { ElMessage } from "element-plus";
export default {
  data() {
    const validatePass2 = (rule, confirmPass, callback) => {
      if (confirmPass === "") {
        callback(new Error("请再次输入密码"));
      } else if (confirmPass !== this.user.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      user: {
        ...this.$store.state.user.user,
        password: "",  // 初始化密码为空
        confirmPass: "",  // 初始化重复密码为空
      },
      rules: {
        email: [
          { required: true, message: "邮箱不能为空", trigger: "blur" },
        ],
        name: [
          { required: true, message: "昵称不能为空", trigger: "blur" },
          {
            min: 2,
            max: 8,
            message: "长度在 2 到 8 个字",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字",
            trigger: "blur",
          },
        ],
        confirmPass: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    userName() {
      return this.$store.state.user?.user?.username || '';
    },
    Email() {
      return this.$store.state.user?.user?.email || '';
    }
  },
  methods: {
    update() {
      this.$refs["updateRef"].validate((valid) => {
        if (valid) {
          // 验证通过
          request.put("/user/update", this.user).then((res) => {
            if (res.code === "200") {
              this.$store.dispatch("user/getLoginUser", this.user);
              this.$router.push("/modify");
              ElMessage.success("重置成功！");
            } else {
              ElMessage.error("重置失败");
            }
          });
        }
      });
    },
  },
};
</script>

<style scoped>
/* 整体表单容器 */
.form-container {
  width: 100%;
  max-width: 800px;
  margin: 40px auto;
  padding: 30px;
  background-color: #ffffff;
  border-radius: 8px;
  box-shadow: 0 2px 15px rgba(0, 0, 0, 0.1);
}

/* 表单标题 */
.form-header {
  font-size: 24px;
  margin-bottom: 30px;
  font-weight: bold;
  color: #333;
}

/* 每个表单项 */
.el-form-item {
  margin-bottom: 20px;
}

/* 输入框 */
.el-input {
  border-radius: 4px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
}

/* 提交按钮 */
.submit-btn {
  width: 100%;
  height: 40px;
  font-size: 16px;
  background-color: #409EFF;
  color: white;
  border-radius: 4px;
  transition: background-color 0.3s ease;
}

/* 提交按钮悬停效果 */
.submit-btn:hover {
  background-color: #66b1ff;
}

/* 表单输入框焦点样式 */
.el-input:focus {
  box-shadow: 0 0 5px rgba(64, 158, 255, 0.5);
  border-color: #409eff;
}

/* 响应式布局：小屏幕下调整输入框宽度 */
@media (max-width: 768px) {
  .el-input {
    width: 100%;
  }
  .submit-btn {
    width: 100%;
  }
}
</style>
